package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.TblUserMapper;
import com.kgc.entity.TblUser;
import com.kgc.entity.TblUserExample;
import com.kgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TblUserMapper  userMapper;
    public void save(TblUser user) {
        userMapper.insertSelective(user);
    }

    public void delete(Long id) {

        userMapper.deleteByPrimaryKey(id.intValue());
    }

    public void delete(String name) {

    }

    public void udpate(TblUser user) {
       userMapper.updateByPrimaryKeySelective(user);
    }

    public TblUser findById(Long id) {
        return userMapper.selectByPrimaryKey(id.intValue());
    }

    public List<TblUser> findAll() {
        return userMapper.selectByExample(null);
    }

    public List<TblUser> findAll(TblUser user) {
        TblUserExample userExample=new TblUserExample();
        TblUserExample.Criteria criteria = userExample.createCriteria();
        if(!StringUtils.isEmpty(user.getName())){
              criteria.andNameEqualTo(user.getName());
          }
          return userMapper.selectByExample(userExample);
    }

    public PageInfo<TblUser> findAll(int page, int pageSize) {
        //设分页面信息
        PageHelper.startPage(page,pageSize);
        List<TblUser> list=userMapper.selectByExample(new TblUserExample());
        PageInfo<TblUser> pageInfo=new PageInfo<TblUser>(list);
        return pageInfo;
    }

    public void dels(Long[] ids) {
        for(Long id:ids)
            delete(id);
    }
}
